
from celery import Celery

from tools.constant.redis_keys import *
#
# # 创建 Celery 实例
app = Celery('tasks',
             broker='redis://:%s@%s:%d/%d' % (REDIS_PWD, REDIS_HOST, REDIS_PORT, REDIS_DB),
             backend='redis://:%s@%s:%d/%d' % (REDIS_PWD, REDIS_HOST, REDIS_PORT, REDIS_DB),
             include=["tasks"])
# 配置 Celery
app.conf.update(
    broker_connection_retry_on_startup=True,
    result_expires=360,  # 结果过期时间
    task_serializer='json',  # 任务序列化方式
    result_serializer='json',  # 结果序列化方式
    accept_content=['json'],  # 接受的内容类型
    timezone='Asia/Shanghai',  # 时区
    worker_concurrency=8,
    worker_prefetch_multiplier=4,
    worker_max_tasks_per_child=200,
    enable_utc=True,  # 启用 UTC
)

def load_celery():
    app = Celery('tasks',
             broker='redis://:%s@%s:%d/%d' % (REDIS_PWD, REDIS_HOST, REDIS_PORT, REDIS_DB),
             backend='redis://:%s@%s:%d/%d' % (REDIS_PWD, REDIS_HOST, REDIS_PORT, REDIS_DB),
             include=["tasks"])
# 配置 Celery
    app.conf.update(
        worker_concurrency=8,
        worker_prefetch_multiplier=4,
        worker_max_tasks_per_child=200,
        broker_connection_retry_on_startup=True,
        result_expires=360,  # 结果过期时间
        task_serializer='json',  # 任务序列化方式
        result_serializer='json',  # 结果序列化方式
        accept_content=['json'],  # 接受的内容类型
        timezone='Asia/Shanghai',  # 时区
        enable_utc=True,  # 启用 UTC
    )
    return app

if __name__ == '__main__':
    load_celery()
    print("celery start")